package com.red5.chat;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.red5.server.adapter.MultiThreadedApplicationAdapter;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
import org.red5.server.api.Red5;
import org.red5.server.api.so.ISharedObject;

public class Application extends MultiThreadedApplicationAdapter
{
	public Application(){
		System.out.println("Main - Chat Project Running");
	}
	
	
	@Override
	public boolean connect(IConnection conn, IScope app, Object[] params)
	{
		return true;//super.appConnect(conn, params);
	}
	
	@Override
	public boolean roomStart(IScope room) 
	{
		this.createSharedObject(room, "RoomTSO", false);
		return super.roomStart(room);            
	}
	
	@Override
	public boolean roomConnect(IConnection conn, Object[] roomUserInfo)
	{
		return super.roomConnect(conn, roomUserInfo);
	}
	
	@Override
	public void disconnect(IConnection conn, IScope scope){
		super.disconnect(conn, scope);
	}
	
	public void sendMessageToRoom( Map<String, Object> chatMessageInfoMap)
	{ 
		int keyId=0;
		ISharedObject chatMessageTSO = getSharedObject(Red5.getConnectionLocal().getScope(), "RoomTSO", false);
		Map<String, Object> chatMessageInfoMapTemp = new ConcurrentHashMap<String,Object>();		
		
		if(chatMessageTSO.getAttribute("ChatMessageInfoMap") == null)			
		{
			  keyId=0;
		}
		else 
		{				
			chatMessageInfoMapTemp = (Map<String, Object>) chatMessageTSO.getAttribute("ChatMessageInfoMap");
			keyId = chatMessageInfoMapTemp.size();
		}						
		chatMessageInfoMapTemp.put(new Integer(keyId).toString(), chatMessageInfoMap);
		chatMessageTSO.setAttribute("ChatMessageInfoMap", chatMessageInfoMapTemp);
	}
}